package cn.zwxky.android.staff.Utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.zgscwjm.lsfbbasetemplate.utils.T;

/**
 * 6.0权限请求
 * Created by Administrator on 2016/7/4 0004.
 */
public class Permission extends Activity {

    private final Context context;
    public boolean isYes1;
    public boolean isYes2;
    public boolean isYes3;
    public boolean isYes4;
    public boolean isYes5;
    public boolean isYes6;

    public Permission(Context context) {
        this.context = context;
    }

    /**
     * 相机权限的请求
     */
    public void GetCAMEARPermission() {

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA},
                    1);

        } else {
            Log.e("nonsense", "相机开启");
            isYes1 = true;
        }
    }

    /**
     * 音频权限设置
     */
    public void GetPermission() {
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.RECORD_AUDIO},
                    2);

        } else {
            Log.e("nonsense", "音频录制开启");
            isYes2 = true;
        }
    }

    /**
     * 网络权限设置
     */
    public void GetMeshPermission() {

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_NETWORK_STATE},
                    3);


        } else {
            Log.e("nonsense", "网络状态开启");
            isYes3 = true;
            GetLocationPermission();
        }
    }


    /**
     * 定位权限设置
     */
    public void GetLocationPermission() {

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    4);


        } else {
            isYes4 = true;
            Log.e("nonsense", "定位状态开启");
        }
    }


    /**
     * 定位权限设置
     */
    public void GetLocationPermission2() {

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    7);


        } else {
            isYes4 = true;
            Log.e("nonsense", "定位状态开启");
        }
    }


    /**
     * 外部存储器权限请求
     */
    public void GetAddressBookPermission() {
        //开启电话簿的权限请求
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 5);
        } else {
            isYes5 = true;
            Log.e("nonsense", "外部存储器权限开启");
        }

    }

    /**
     *
     */
    public void GetwriteSettingsPermission() {
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_SETTINGS}, 6);
        } else {
            isYes6 = true;
            Log.e("nonsense", "读写权限开启");
        }

    }

    /**
     *打电话
     */
    public void GetcallPermission() {
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, 8);
        } else {
            isYes6 = true;
            Log.e("nonsense", "读写权限开启");
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    T.showShort(context, "请求权限成功");
                    isYes1 = true;
                } else {

                    T.showShort(context, "请求权限失败");
                    isYes1 = false;
                    GetCAMEARPermission();
                }
                break;
            case 2:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    T.showShort(context, "请求权限成功");
                    isYes2 = true;
                } else {

                    T.showShort(context, "请求权限失败");
                    isYes2 = false;
                    GetPermission();
                }
                break;
            case 3:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    T.showShort(context, "请求权限成功");
                    isYes3 = true;
                } else {

                    T.showShort(context, "请求权限失败");
                    isYes3 = false;
                    GetMeshPermission();
                }
                break;
            case 4:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    T.showShort(context, "请求权限成功");
                    isYes4 = true;
                } else {

                    T.showShort(context, "请求权限失败");

                    isYes4 = false;
                    GetLocationPermission();
                }
                break;
            case 5:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    T.showShort(context, "请求权限成功");
                    isYes5 = true;
                } else {

                    T.showShort(context, "请求权限失败");
                    isYes5 = false;
                    GetAddressBookPermission();
                }
                break;
            case 6:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    T.showShort(context, "请求权限成功");
                    isYes6 = true;
                } else {

                    T.showShort(context, "请求权限失败");
                    isYes6 = false;
                    GetAddressBookPermission();
                }
                break;
            case 7:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    T.showShort(context, "请求权限成功");
                } else {

                    T.showShort(context, "请求权限失败");
                }
                break;
            case 8:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    T.showShort(context, "请求权限成功");
                } else {

                    T.showShort(context, "请求权限失败");
                }
                break;
        }
    }

}
